
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>内存优化 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="opt-network.html" />
    
    
    <link rel="prev" href="service.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">----安卓 UI----</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="view.html">
            
                <a href="view.html">
            
                    
                    自定义控件和measure
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="canvas-paint.html">
            
                <a href="canvas-paint.html">
            
                    
                    draw和canvas，paint详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="anim.html">
            
                <a href="anim.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="notify.html">
            
                <a href="notify.html">
            
                    
                    notify系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="loading.html">
            
                <a href="loading.html">
            
                    
                    loading系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="status-ui.html">
            
                <a href="status-ui.html">
            
                    
                    StatusUI系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="touch-scroll.html">
            
                <a href="touch-scroll.html">
            
                    
                    滑动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="layout.html">
            
                <a href="layout.html">
            
                    
                    Layout
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="textview.html">
            
                <a href="textview.html">
            
                    
                    TextView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="drawable.html">
            
                <a href="drawable.html">
            
                    
                    Drawable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="item-template.html">
            
                <a href="item-template.html">
            
                    
                    列表Adapter模板化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="recyclerview.html">
            
                <a href="recyclerview.html">
            
                    
                    RecyclerView全解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="design.html">
            
                <a href="design.html">
            
                    
                    MeterialDesign详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="component.html">
            
                <a href="component.html">
            
                    
                    AyoComponent基础库--Activity和Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="systembartint.html">
            
                <a href="systembartint.html">
            
                    
                    SystembarTint
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="permission-ask.html">
            
                <a href="permission-ask.html">
            
                    
                    权限控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="keyboard.html">
            
                <a href="keyboard.html">
            
                    
                    小键盘
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="notification.html">
            
                <a href="notification.html">
            
                    
                    Notification
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----安卓 foundation----</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="mvp.html">
            
                <a href="mvp.html">
            
                    
                    MVP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="rxjava.html">
            
                <a href="rxjava.html">
            
                    
                    RxJava
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="lang.html">
            
                <a href="lang.html">
            
                    
                    常用工具类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="http.html">
            
                <a href="http.html">
            
                    
                    http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="xml.html">
            
                <a href="xml.html">
            
                    
                    xml解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="json-reporter.html">
            
                <a href="json-reporter.html">
            
                    
                    Json解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="inject.html">
            
                <a href="inject.html">
            
                    
                    ButterKnife
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="thread.html">
            
                <a href="thread.html">
            
                    
                    安卓线程和looper
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="process.html">
            
                <a href="process.html">
            
                    
                    多进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="eventbus.html">
            
                <a href="eventbus.html">
            
                    
                    EventBus
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="db.html">
            
                <a href="db.html">
            
                    
                    数据库和ContentProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="job.html">
            
                <a href="job.html">
            
                    
                    定时任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="service.html">
            
                <a href="service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.14" data-path="opt-mem.html">
            
                <a href="opt-mem.html">
            
                    
                    内存优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.15" data-path="opt-network.html">
            
                <a href="opt-network.html">
            
                    
                    网络优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16" data-path="opt-anr.html">
            
                <a href="opt-anr.html">
            
                    
                    anr排查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.17" data-path="opt-ui.html">
            
                <a href="opt-ui.html">
            
                    
                    UI卡顿优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.18" data-path="opt-launch.html">
            
                <a href="opt-launch.html">
            
                    
                    启动优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.19" data-path="opt-launch.html">
            
                <a href="opt-launch.html">
            
                    
                    混淆
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.20" data-path="hotfix.html">
            
                <a href="hotfix.html">
            
                    
                    热补丁混淆
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.21" data-path="aar.html">
            
                <a href="aar.html">
            
                    
                    aar相关
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.22" data-path="route.html">
            
                <a href="route.html">
            
                    
                    Route路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.23" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    分模块，组件化，插件化
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----Gradle----</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="gradle.html">
            
                <a href="gradle.html">
            
                    
                    gradle基本配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="gradle-plugin.html">
            
                <a href="gradle-plugin.html">
            
                    
                    gradle插件开发
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----工具----</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="repos.html">
            
                <a href="repos.html">
            
                    
                    优秀博客，开源项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="packs.html">
            
                <a href="packs.html">
            
                    
                    自动打包，多渠道打包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="gist.html">
            
                <a href="gist.html">
            
                    
                    代码片段和常见问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="stetho.html">
            
                <a href="stetho.html">
            
                    
                    stetho
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="fabric.html">
            
                <a href="fabric.html">
            
                    
                    fabric
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="app-update.html">
            
                <a href="app-update.html">
            
                    
                    自动升级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="bugsnags.html">
            
                <a href="bugsnags.html">
            
                    
                    日志和崩溃统计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="mail.html">
            
                <a href="mail.html">
            
                    
                    发邮件辅助接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="socail.html">
            
                <a href="socail.html">
            
                    
                    第三方登录和分享
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="pay-alipay.html">
            
                <a href="pay-alipay.html">
            
                    
                    支付宝支付
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="pay-wx.html">
            
                <a href="pay-wx.html">
            
                    
                    微信支付
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.12" data-path="umeng.html">
            
                <a href="umeng.html">
            
                    
                    友盟统计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.13" data-path="push-jpush.html">
            
                <a href="push-jpush.html">
            
                    
                    推送-极光
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.14" data-path="push-xiaomi.html">
            
                <a href="push-xiaomi.html">
            
                    
                    推送-小米
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >内存优化</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5185;&#x5B58;&#x4F18;&#x5316;">&#x5185;&#x5B58;&#x4F18;&#x5316;</h1>
<h2 id="1-&#x9759;&#x6001;activity&#x548C;view">1 &#x9759;&#x6001;Activity&#x548C;View</h2>
<p>&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x6700;&#x597D;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x5C31;&#x662F;&#xFF1A;&#x4E0D;&#x8981;&#x8FD9;&#x4E48;&#x5199;&#x4EE3;&#x7801;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span> </span>{

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> Context context;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> TextView textView;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = <span class="hljs-keyword">this</span>;
        textView = <span class="hljs-keyword">new</span> TextView(<span class="hljs-keyword">this</span>);
    }
}
</code></pre>
<h2 id="2-&#x5185;&#x90E8;&#x7C7B;&#x548C;&#x533F;&#x540D;&#x5185;&#x90E8;&#x7C7B;">2 &#x5185;&#x90E8;&#x7C7B;&#x548C;&#x533F;&#x540D;&#x5185;&#x90E8;&#x7C7B;</h2>
<p>&#x5185;&#x90E8;&#x7C7B;&#x548C;&#x533F;&#x540D;&#x5185;&#x90E8;&#x7C7B;&#x4F1A;&#x6301;&#x6709;&#x5916;&#x90E8;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;</p>
<p>&#x7279;&#x522B;&#x662F;Thread&#x548C;Handler&#xFF0C;&#x56E0;&#x4E3A;&#x6D89;&#x53CA;&#x5230;&#x5EF6;&#x65F6;&#xFF0C;&#x5E76;&#x4E14;&#x90FD;&#x9700;&#x8981;&#x4E3B;&#x52A8;&#x5173;&#x95ED;&#xFF0C;&#x6240;&#x4EE5;&#x5F88;&#x5BB9;&#x6613;&#x4EA7;&#x751F;&#x5185;&#x5B58;&#x6CC4;&#x9732;</p>
<ul>
<li>&#x4F7F;&#x7528;&#x9759;&#x6001;&#x5185;&#x90E8;&#x7C7B;&#xFF0C;&#x914D;&#x5408;WeakReference</li>
</ul>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LeakActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span> </span>{

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak);
        leakFun();
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">leakFun</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> Runnable() {
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
                <span class="hljs-keyword">try</span> {
                    Thread.sleep(Integer.MAX_VALUE);
                } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
</code></pre>
<h2 id="3-&#x52A8;&#x753B;">3 &#x52A8;&#x753B;</h2>
<p>&#x5728;OnDestroy&#x4E2D;&#x505C;&#x6B62;&#x52A8;&#x753B;
&#x6216;&#x8005;&#x5728;View&#x7684;&#x67D0;&#x4E2A;&#x751F;&#x547D;&#x5468;&#x671F;&#x91CC;&#x505C;&#x6B62;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LeakActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span> </span>{

    <span class="hljs-keyword">private</span> TextView textView;
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak);
        textView = (TextView)findViewById(R.id.text_view);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,<span class="hljs-string">&quot;rotation&quot;</span>,<span class="hljs-number">0</span>,<span class="hljs-number">360</span>);
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimator.start();
    }
}
</code></pre>
<h2 id="4-handler&#x6CC4;&#x9732;">4 Handler&#x6CC4;&#x9732;</h2>
<p>&#x8FD9;&#x4E2A;&#x662F;&#x8001;&#x751F;&#x5E38;&#x8C08;&#x7684;&#x95EE;&#x9898;</p>
<p>&#x89E3;&#x51B3;&#x65B9;&#x6848;1&#xFF1A;&#x505A;&#x6210;&#x9759;&#x6001;&#x5185;&#x90E8;&#x7C7B;&#xFF0C;&#x914D;&#x5408;WeakReference</p>
<pre><code class="lang-java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyHandler</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Handler</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> WeakReference&lt;SampleActivity&gt; mActivity;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">MyHandler</span><span class="hljs-params">(SampleActivity activity)</span> </span>{
      mActivity = <span class="hljs-keyword">new</span> WeakReference&lt;SampleActivity&gt;(activity);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleMessage</span><span class="hljs-params">(Message msg)</span> </span>{
      SampleActivity activity = mActivity.get();
      <span class="hljs-keyword">if</span> (activity != <span class="hljs-keyword">null</span>) {
        <span class="hljs-comment">// ...</span>
      }
    }
  }
</code></pre>
<p>&#x89E3;&#x51B3;&#x65B9;&#x6848;2&#xFF1A;onDestroy&#x65F6;&#xFF0C;&#x79FB;&#x9664;&#x6240;&#x6709;&#x6D88;&#x606F;&#x548C;&#x56DE;&#x8C03;</p>
<pre><code class="lang-java">handler.removeCallbacksAndMessages(<span class="hljs-keyword">null</span>);
</code></pre>
<h2 id="5-&#x9700;&#x8981;&#x6CE8;&#x518C;&#x7684;&#xFF0C;ondestroy&#x65F6;&#x90FD;&#x8981;&#x6CE8;&#x9500;">5 &#x9700;&#x8981;&#x6CE8;&#x518C;&#x7684;&#xFF0C;onDestroy&#x65F6;&#x90FD;&#x8981;&#x6CE8;&#x9500;</h2>
<p>&#x5982;EventBus, RxJava&#xFF0C;&#x5E7F;&#x64AD;</p>
<h2 id="6-&#x4F60;&#x4E3B;&#x52A8;&#x5F00;&#x542F;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x91CC;&#x7684;&#x4EFB;&#x52A1;">6 &#x4F60;&#x4E3B;&#x52A8;&#x5F00;&#x542F;&#x7684;&#x7EBF;&#x7A0B;&#x6C60;&#x91CC;&#x7684;&#x4EFB;&#x52A1;</h2>
<h2 id="7-http&#xFF0C;imageloader&#x4EFB;&#x52A1;">7 Http&#xFF0C;ImageLoader&#x4EFB;&#x52A1;</h2>
<h2 id="8-&#x81EA;&#x5DF1;&#x5199;&#x4E00;&#x4E2A;&#x53EF;&#x5173;&#x95ED;&#x8D44;&#x6E90;&#x6C60;&#xFF0C;&#x7EDF;&#x4E00;&#x7BA1;&#x7406;&#x4E00;&#x4E2A;&#x754C;&#x9762;&#x91CC;context&#x91CC;&#x7684;&#x53EF;&#x80FD;&#x9020;&#x6210;&#x5185;&#x5B58;&#x6CC4;&#x9732;&#x7684;&#x8D44;&#x6E90;">8 &#x81EA;&#x5DF1;&#x5199;&#x4E00;&#x4E2A;&#x53EF;&#x5173;&#x95ED;&#x8D44;&#x6E90;&#x6C60;&#xFF0C;&#x7EDF;&#x4E00;&#x7BA1;&#x7406;&#x4E00;&#x4E2A;&#x754C;&#x9762;&#x91CC;Context&#x91CC;&#x7684;&#x53EF;&#x80FD;&#x9020;&#x6210;&#x5185;&#x5B58;&#x6CC4;&#x9732;&#x7684;&#x8D44;&#x6E90;</h2>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x5185;&#x90E8;&#x7C7B;&#x4F60;&#x6CA1;&#x6CD5;&#x7BA1;&#x7406;&#xFF0C;&#x662F;&#x5E95;&#x5C42;&#x673A;&#x5236;&#x51B3;&#x5B9A;&#x4E86;&#x5185;&#x90E8;&#x7C7B;&#x5BF9;&#x8C61;&#x5FC5;&#x987B;&#x6301;&#x6709;&#x5916;&#x90E8;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;</p>
<pre><code class="lang-java">&#x9996;&#x5148;&#x5B9A;&#x4E49;&#x4E00;&#x7EC4;&#x7C7B;&#xFF0C;&#x6765;&#x8868;&#x793A;&#x53EF;&#x4EE5;&#x88AB;&#x5173;&#x95ED;&#x7684;&#x8D44;&#x6E90;

<span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Closesable</span>&lt;<span class="hljs-title">T</span>&gt; </span>{

    <span class="hljs-keyword">private</span> WeakReference&lt;T&gt; closeable;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Closesable</span><span class="hljs-params">(T t)</span></span>{
        <span class="hljs-keyword">this</span>.closeable = <span class="hljs-keyword">new</span> WeakReference&lt;T&gt;(t);;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">close</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">final</span> T t = closeable.get();
        <span class="hljs-keyword">if</span>(t != <span class="hljs-keyword">null</span>){
            closeMe(t);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">abstract</span>  <span class="hljs-keyword">void</span> <span class="hljs-title">closeMe</span><span class="hljs-params">(T t)</span></span>;
}


<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AnimatorCloseable</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Closesable</span>&lt;<span class="hljs-title">Animator</span>&gt; </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">AnimatorCloseable</span><span class="hljs-params">(Animator animator)</span> </span>{
        <span class="hljs-keyword">super</span>(animator);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">closeMe</span><span class="hljs-params">(Animator animator)</span> </span>{
        <span class="hljs-keyword">if</span>(animator.isRunning()) animator.cancel();
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HandlerCloseable</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Closesable</span>&lt;<span class="hljs-title">Handler</span>&gt; </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">HandlerCloseable</span><span class="hljs-params">(Handler handler)</span> </span>{
        <span class="hljs-keyword">super</span>(handler);
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">closeMe</span><span class="hljs-params">(Handler handler)</span> </span>{
        handler.removeCallbacksAndMessages(<span class="hljs-keyword">null</span>);
    }
}

&#x7136;&#x540E;&#xFF0C;&#x6BCF;&#x4E2A;&#x5B9A;&#x4E49;&#x8D44;&#x6E90;&#x6C60;
&#x8D44;&#x6E90;&#x6216;&#x4EFB;&#x52A1;&#x6253;&#x5F00;&#x65F6;&#xFF0C;&#x653E;&#x5165;&#x8D44;&#x6E90;&#x6C60;
&#x8D44;&#x6E90;&#x4F7F;&#x7528;&#x7ED3;&#x675F;&#xFF0C;&#x6216;&#x8005;&#x4EFB;&#x52A1;&#x8FD0;&#x884C;&#x7ED3;&#x675F;&#xFF0C;&#x4ECE;&#x8D44;&#x6E90;&#x6C60;&#x79FB;&#x9664;
&#x8D44;&#x6E90;&#x548C;&#x4EFB;&#x52A1;&#x88AB;&#x7528;&#x6237;&#x4E3B;&#x52A8;&#x5173;&#x95ED;&#xFF0C;&#x4ECE;&#x8D44;&#x6E90;&#x6C60;&#x79FB;&#x9664;
&#x4E0A;&#x4E0B;&#x6587;&#x9500;&#x6BC1;&#x65F6;&#xFF0C;&#x8D44;&#x6E90;&#x6C60;&#x5173;&#x95ED;&#x6240;&#x6709;&#x8D44;&#x6E90;


<span class="hljs-keyword">import</span> android.app.Activity;
<span class="hljs-keyword">import</span> android.app.Application;
<span class="hljs-keyword">import</span> android.app.Service;
<span class="hljs-keyword">import</span> android.support.v4.app.Fragment;

<span class="hljs-keyword">import</span> java.util.HashMap;
<span class="hljs-keyword">import</span> java.util.Map;

<span class="hljs-comment">/**
 */</span>

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LeakPool</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> LeakPool <span class="hljs-title">fromContext</span><span class="hljs-params">(Activity context)</span></span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> LeakPool();
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> LeakPool <span class="hljs-title">fromContext</span><span class="hljs-params">(Fragment context)</span></span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> LeakPool();
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> LeakPool <span class="hljs-title">fromContext</span><span class="hljs-params">(android.app.Fragment context)</span></span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> LeakPool();
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> LeakPool <span class="hljs-title">fromContext</span><span class="hljs-params">(Service context)</span></span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> LeakPool();
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> LeakPool <span class="hljs-title">fromContext</span><span class="hljs-params">(Application context)</span></span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> LeakPool();
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">LeakPool</span><span class="hljs-params">()</span></span>{

    }

    <span class="hljs-keyword">private</span> Map&lt;String, Closesable&lt;?&gt;&gt; pool = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();

    <span class="hljs-keyword">public</span> &lt;T&gt; <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(String tag, Closesable&lt;T&gt; closesable)</span></span>{
        <span class="hljs-keyword">if</span>(pool.containsKey(tag)){
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> RuntimeException(<span class="hljs-string">&quot;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x4EFB;&#x52A1;&#xFF1A;&quot;</span> + tag);
        }<span class="hljs-keyword">else</span>{
            pool.put(tag, closesable);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">closeAndRemove</span><span class="hljs-params">(String tag)</span></span>{
        Closesable&lt;?&gt; closesable = pool.get(tag);
        <span class="hljs-keyword">if</span>(closesable != <span class="hljs-keyword">null</span>){
            closesable.close();
            pool.remove(tag);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">(String tag)</span></span>{
        Closesable&lt;?&gt; closesable = pool.get(tag);
        <span class="hljs-keyword">if</span>(closesable != <span class="hljs-keyword">null</span>){
            pool.remove(tag);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">closeAll</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">for</span>(Closesable&lt;?&gt; closesable : pool.values()){
            closesable.close();
        }
        pool.clear();
    }

}
</code></pre>
<h2 id="9-mat&#x5206;&#x6790;&#x5185;&#x5B58;&#x6CC4;&#x9732;">9 MAT&#x5206;&#x6790;&#x5185;&#x5B58;&#x6CC4;&#x9732;</h2>
<p><a href="http://blog.csdn.net/ljd2038/article/details/53560829" target="_blank">http://blog.csdn.net/ljd2038/article/details/53560829</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="service.html" class="navigation navigation-prev " aria-label="Previous page: service">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="opt-network.html" class="navigation navigation-next " aria-label="Next page: 网络优化">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"内存优化","level":"2.14","depth":1,"next":{"title":"网络优化","level":"2.15","depth":1,"path":"opt-network.md","ref":"opt-network.md","articles":[]},"previous":{"title":"service","level":"2.13","depth":1,"path":"service.md","ref":"service.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"opt-mem.md","mtime":"2017-04-09T17:00:38.458Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-04-09T17:03:25.521Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

